//
//  RegistViewController.m
//  NewChat
//
//  Created by songKX on 16/3/3.
//  Copyright © 2016年 蓝鸥. All rights reserved.
//

#import "RegistViewController.h"

//#import <AVOSCloud/AVOSCloud.h>

#import "DetailViewController.h"

@interface RegistViewController ()
@property (weak, nonatomic) IBOutlet UITextField *phoneNumber;

@property (weak, nonatomic) IBOutlet UITextField *MesCode;


@property (weak, nonatomic) IBOutlet UITextField *passWord;



@end

@implementation RegistViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
//    self.navigationItem.rightBarButtonItem.enabled = NO;
    
    [self.passWord addTarget:self action:@selector(textFieldDidChange:) forControlEvents:UIControlEventEditingChanged];
    
    
    
    
    // Do any additional setup after loading the view.
}


//实现方法
- (void) textFieldDidChange:(UITextField *) sender {
    
    
    if (sender.text.length >= 6) {
        
        self.navigationItem.rightBarButtonItem.enabled = YES;
        
    }
    
    
}


//发送验证码

- (IBAction)sendMessageCode:(UIButton *)sender {
    
    
    if (self.phoneNumber.text.length != 11) {
        
        UIAlertAction *act = [UIAlertAction actionWithTitle:@"确定" style:(UIAlertActionStyleDefault) handler:^(UIAlertAction * _Nonnull action) {
            
            self.phoneNumber.text = nil;
            
        }];
        
        UIAlertController *con = [UIAlertController alertControllerWithTitle:@"提示" message:@"请输入正确的手机号码" preferredStyle:(UIAlertControllerStyleAlert)];
        
        [con addAction:act];
        [self presentViewController:con animated:YES completion:nil];
    }
    else{
        
        
        [AVOSCloud requestSmsCodeWithPhoneNumber:self.phoneNumber.text callback:^(BOOL succeeded, NSError *error) {
            
            
            if (error==nil) {
                
                UIAlertAction *act = [UIAlertAction actionWithTitle:@"确定" style:(UIAlertActionStyleDefault) handler:^(UIAlertAction * _Nonnull action) {
                    
                    //                    self.phoneNumber.text = nil;
                    
                }];
                
                UIAlertController *con = [UIAlertController alertControllerWithTitle:@"提示" message:@"验证码已经发送" preferredStyle:(UIAlertControllerStyleAlert)];
                
                [con addAction:act];
                [self presentViewController:con animated:YES completion:nil];
                
            }else{
                
                
                UIAlertAction *act = [UIAlertAction actionWithTitle:@"确定" style:(UIAlertActionStyleDefault) handler:^(UIAlertAction * _Nonnull action) {
                    
                    //                    self.phoneNumber.text = nil;
                    
                }];
                
                NSString *message;
                
                if (error.code == 127) {
                    
                    message = @"手机号不存在";
                    
                }else if (error.code == 601){
                    
                    message = @"手机号达到最大验证次数";
                    
                    
                }
                
                
                UIAlertController *con = [UIAlertController alertControllerWithTitle:@"提示" message:message preferredStyle:(UIAlertControllerStyleAlert)];
                
                [con addAction:act];
                [self presentViewController:con animated:YES completion:nil];
                
            }
            
        }];
    }
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    
    // Dispose of any resources that can be recreated.
}

- (IBAction)turnToDetail:(UIBarButtonItem *)sender {
    
    NSLog(@"验证码验证，准备页面跳转");
    
        DetailViewController *temp = [[DetailViewController alloc] init];
    
        temp.userName = self.phoneNumber.text;
    
        temp.userPasswd = self.passWord.text;
    
        [self.navigationController pushViewController:temp animated:YES];
    
//    [AVOSCloud verifySmsCode:self.MesCode.text mobilePhoneNumber:self.phoneNumber.text callback:^(BOOL succeeded, NSError *error) {
//        
//        if (succeeded) {
//            
//            DetailViewController *temp = [[DetailViewController alloc] init];
//            
//            
//            temp.userName = self.phoneNumber.text;
//            
//            temp.userPasswd = self.passWord.text;
//            
//            [self.navigationController pushViewController:temp animated:YES];
//            
//            
//        }else{
//            
//            UIAlertAction *act = [UIAlertAction actionWithTitle:@"确定" style:(UIAlertActionStyleDefault) handler:^(UIAlertAction * _Nonnull action) {
//                
//                self.MesCode.text = @"";
//                
//            }];
//            
//            UIAlertController *con = [UIAlertController alertControllerWithTitle:@"提示" message:@"验证码不正确" preferredStyle:(UIAlertControllerStyleAlert)];
//            
//            [con addAction:act];
//            [self presentViewController:con animated:YES completion:nil];
//            
//            
//        }
//        
//    }];

    
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
   
    
}
*/
@end
